package com.dengyafei.controller;

import com.dengyafei.annotation.JwtToken;
import com.dengyafei.bean.PositionEntity;
import com.dengyafei.constant.RecommendConstant;
import com.dengyafei.constant.ResultMessage;
import com.dengyafei.domin.dto.PositionAverageSalaryCityDTO;
import com.dengyafei.domin.dto.PositionMaxSalaryDTO;
import com.dengyafei.domin.dto.PreferScorePositionDTO;
import com.dengyafei.domin.vo.*;
import com.dengyafei.result.PageEntity;
import com.dengyafei.result.RestApiResult;
import com.dengyafei.result.SearchPositionEntity;
import com.dengyafei.service.PositionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 邓亚非
 */
@RestController
@RequestMapping("/position")
@Slf4j
public class PositionController {

    @Autowired
    PositionService positionService;

    /**
     * 根据hid查询职位信息
     *
     * @param pageVo
     * @return
     */
    @JwtToken
    @PostMapping(value = "/queryPositionByHid")
    public RestApiResult queryPositionByHid(@RequestBody PageVo pageVo) {
        log.info("【职位模块-根据hid查询职业接口-参数】pageVo:[{}]", pageVo);
        PageEntity<PositionEntity> pageEntity = positionService.queryByHid(pageVo);
        log.info("【职位模块-根据hid查询职位接口-成功】pageVo:[{}]", ResultMessage.Position.QUERY_BY_HID_SUCCESS);
        return RestApiResult.ok(pageEntity);
    }

    /**
     * 添加一个职位
     *
     * @param positionInsertVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/addOnePosition")
    public RestApiResult addOnePosition(@RequestBody PositionInsertVO positionInsertVO) {
        log.info("【职位模块-添加职位接口-参数】positionInsertVO:[{}]", positionInsertVO);
        Integer integer = positionService.insertOnePosition(positionInsertVO);
        if (integer.equals(RecommendConstant.POSITION_ADD_FAILURE)) {
            log.error("【职位模块-添加职位接口-失败】positionInsertVO,cause:[{}]", ResultMessage.Position.ADD_POSITION_FAILURE);
            return RestApiResult.error(ResultMessage.Position.ADD_POSITION_FAILURE);
        }
        log.info("【职位模块-添加职位接口-成功】positionInsertVO:[{}]", ResultMessage.Position.ADD_POSITION_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.Position.ADD_POSITION_SUCCESS);
    }

    /**
     * 根据职位名称搜索
     *
     * @param positionQueryByNameVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/queryPositionByName")
    public RestApiResult queryPositionByName(@RequestBody PositionQueryByNameVO positionQueryByNameVO) {
        log.info("【职位模块-搜索接口-参数】positionQueryByNameVO:[{}]", positionQueryByNameVO);
        PageEntity<PositionEntity> pageEntity = positionService.queryByName(positionQueryByNameVO);
        log.info("【职位模块-搜索接口-成功】positionQueryByNameVO:[{}]", ResultMessage.Position.SEARCH_SUCCESS);
        return RestApiResult.ok(pageEntity);
    }

    /**
     * 编辑接口
     *
     * @param positionEditVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/editPosition")
    public RestApiResult editPosition(@RequestBody PositionEditVO positionEditVO) {
        log.info("【职位模块-编辑接口-参数】positionEditVO:[{}]", positionEditVO);
        Integer integer = positionService.editPosition(positionEditVO);
        if (integer.equals(RecommendConstant.POSITION_EDIT_FAILURE)) {
            log.error("【职位模块-编辑接口-失败】positionEditVO,cause:[{}]", ResultMessage.Position.POSITION_EDIT_FAILURE);
            return RestApiResult.error(ResultMessage.Position.POSITION_EDIT_FAILURE);
        }
        log.info("【职位模块-编辑接口-成功】positionEditVO:[{}]", ResultMessage.Position.POSITION_EDIT_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.Position.POSITION_EDIT_SUCCESS);
    }

    /**
     * 删除职位接口
     *
     * @param positionDeleteVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/deletePosition")
    public RestApiResult deletePosition(@RequestBody PositionDeleteVO positionDeleteVO) {
        log.info("【职位模块-删除职位接口-参数】positionDeleteVO:[{}]", positionDeleteVO);
        Integer integer = positionService.deletePosition(positionDeleteVO);
        if (!integer.equals(RecommendConstant.OPERATION_SUCCESS)) {
            log.error("【职位模块-删除职位接口-失败】positionDeleteVO,cause:[{}]", ResultMessage.Position.POSITION_DELETE_FAILURE);
            return RestApiResult.error(ResultMessage.Position.POSITION_DELETE_FAILURE);
        }
        log.info("【职位模块-删除职位接口-成功】positionDeleteVO:[{}]", ResultMessage.Position.POSITION_DELETE_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.Position.POSITION_DELETE_SUCCESS);
    }

    /**
     * 根据职位名称查询职位信息
     *
     * @param positionQueryByNameVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/searchPositions")
    public RestApiResult searchPositions(@RequestBody PositionQueryByNameVO positionQueryByNameVO) {
        log.info("【职位模块-根据名称查询职位接口-参数】positionQueryByNameVO:[{}]", positionQueryByNameVO);
        PageEntity<PositionEntity> pageEntity = positionService.queryByCondition(positionQueryByNameVO);
        log.info("【职位模块-根据名称查询职位接口-成功】positionQueryByNameVO:[{}]", ResultMessage.Position.POSITION_QUERY_BY_NAME);
        return RestApiResult.ok(pageEntity);
    }

    /**
     * 加载岗位数据接口
     * @param positionLoadVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/loadByName")
    public RestApiResult loadByName(@RequestBody PositionLoadVO positionLoadVO){
        log.info("【职位模块-加载职位接口-参数】positionLoadVO:[{}]",positionLoadVO);
        List<PositionEntity> positionEntities = positionService.queryByHid(positionLoadVO);
        log.info("【职位模块-加载职位接口-成功】positionLoadVO:[{}]",ResultMessage.Position.POSITION_QUERY_BY_NAME);
        return RestApiResult.ok(positionEntities);
    }

    /**
     * 用户评分前五的职位
     * @return
     */
    @PostMapping(value = "/queryScorePosition")
    public RestApiResult queryScorePosition(){
        List<PreferScorePositionDTO> preferScorePositionDTOS = positionService.queryScorePosition();
        if(CollectionUtils.isEmpty(preferScorePositionDTOS)){
            return null;
        }
        return RestApiResult.ok(preferScorePositionDTOS);
    }

    /**
     * 平均薪资排名前五的城市
     * @return
     */
    @PostMapping(value ="/queryAverageCity")
    public RestApiResult queryAverageCity(){
        List<PositionAverageSalaryCityDTO> positionAverageSalaryCityDTOS = positionService.queryPositionAverageSalaryCity();
        if (CollectionUtils.isEmpty(positionAverageSalaryCityDTOS)){
            return null;
        }
        return RestApiResult.ok(positionAverageSalaryCityDTOS);
    }

    /**
     * 薪资最高的前五个职位
     * @return
     */
    @PostMapping(value = "/queryPositionMaxSalary")
    public RestApiResult queryPositionMaxSalary(){
        List<PositionMaxSalaryDTO> positionMaxSalaryDTOS = positionService.queryPositionMaxSalary();
        if (CollectionUtils.isEmpty(positionMaxSalaryDTOS)){
            return null;
        }
        return RestApiResult.ok(positionMaxSalaryDTOS);
    }

    /**
     * 所有职位信息
     * @return
     */
    @PostMapping(value = "/queryPositions")
    public RestApiResult queryPositions(){
        List<PositionEntity> positionEntities = positionService.queryPositions();
        return RestApiResult.ok(positionEntities);
    }
}